<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      body {
        padding-top: 100px;
        background-color: rgb(32, 32, 55);
        display: flex;
        justify-content: center;
      }

      p {
        height: 50px;
        padding-left: 20px;
        line-height: 50px;
      }

      .tab {
        border-radius: 10px;
        overflow: hidden;
      }

      .item {
        list-style: none;
        width: 400px;
        background-color: rgb(255, 255, 255);
      }

      .item > ul > li {
        font-size: 14px;
        border-bottom: 1px solid #b7a5d3;
        padding-left: 20px;
        line-height: 40px;
        color: aliceblue;
        transition: 1s;
        background-color: rgb(59, 59, 101);
      }

      #List ul {
        display: none;
      }

      .active {
        height: 40px;
        opacity: 1;
      }
    </style>
  </head>
  <body>
    <div class="tab">
      <ul id="List">
        <li class="item">
          <p>Starry, starry night</p>
          <ul>
            <li>Lorem ipsum dolor sit amet.</li>
            <li>Lorem ipsum dolor sit amet consectetur.</li>
            <li>Paint your palette blue and gray</li>
            <li>Lorem ipsum dolor sit.</li>
          </ul>
        </li>
        <li class="item">
          <p>They would not listen</p>
          <ul>
            <li>Lorem ipsum dolor sit amet consectetur.</li>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
          </ul>
        </li>
        <li class="item">
          <p>Perhaps they’ll listen now</p>
          <ul>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
          </ul>
        </li>
        <li class="item">
          <p>Reflect in Vincent’s eyes of china blue</p>
          <ul>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
            <li>Paint your palette blue and gray</li>
          </ul>
        </li>
      </ul>
    </div>

    <script></script>
  </body>
  <script>
    //总大表
    let list = document.getElementById("List");
    // 表的每一项
    let aItem = list.getElementsByTagName("li");
    //下拉项
    let aUl = list.getElementsByTagName("ul");
    // 表标题
    let pNode = list.getElementsByTagName("p");
    // 下拉列表的每一小项
    let oLi = document.querySelectorAll(".item li");
    for (let i = 0; i < pNode.length; i++) {
      pNode[i].index = i;
      pNode[i].isFlag = false;
      pNode[i].onclick = function () {
        //如果当前的列表是展开的，则关闭
        if (aUl[this.index].isFlag) {
          for (let j = 0; j < pNode.length; j++) {
            aUl[j].style.display = "none";
            aUl[j].isFlag = false;
          }
        } else {
          // 否则关闭所有列表
          for (let j = 0; j < pNode.length; j++) {
            aUl[j].style.display = "none";
            aUl[j].isFlag = false;
          }
          // 打开所点击的列表
          aUl[this.index].style.display = "block";
          aUl[this.index].isFlag = true;
        }
      };
    }
  </script>
</html>
